const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    hasEmptyGrid: false,
    cur_year: '',
    cur_month: '',
    cur_day: '',
    weeks_ch: '',
    days: '',
    signNumber: 0, //签到人数
    signInList: [],
    signStatus: 0 // 今日是否签到 0未签1签到成功
  },

  onLoad(options) {
    // 获取签到数据
    // 获取签到数据, 后台返回的数据类似于["2019年7月1号"] 
    // 将 2019年7月1号转换成 1  2019年7月2号转化成2
    // ToDo
    var that = this
    util.request(api.UserSignData).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          signNumber: res.data.num,
          signInList: res.data.items
        })
      }
      let list = that.data.signInList
      let signList = []
      for (let i = 0; i < list.length; i++) {
        signList.push(+list[i])
      }
      // 生成日历
      that.setNowDate(signList)
    })

  },

  // 点击签到
  signIn: function(e) {
    const index = new Date().getDate() - 1
    const signToday = "days[" + index + "].isSign"

    this.setData({
      signStatus: 1,
      [signToday]: "is",
    })
    // ToDo
    // 发送消息后台 记录该用户当天已签到
    var that = this
    util.request(api.UserSignIn).then(function(res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '签到成功,获得一积分',
          icon: "none",
          duration: 2000
        })
        that.setData({
          signNumber: +that.data.signNumber + 1
        })
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: "none",
          duration: 2000
        })
      }
    })
  },

  // 设置当前日期
  setNowDate: function(list) {
    const date = new Date()
    const cur_year = date.getFullYear() // 年
    const cur_month = date.getMonth() + 1 // 月
    const cur_day = date.getDate() // 日
    // const todayIndex = date.getDate() - 1 
    let signStatus = list.indexOf(cur_day) > -1 ? 1 : 0
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六']
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month, list);
    this.setData({
      cur_year,
      cur_month,
      cur_day,
      weeks_ch,
      // todayIndex,
      signStatus
    })
  },

  // 获取某年某月的天数
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate()
  },

  // 获取某年每月第一天是周几
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay()
  },

  // 筛选空日期
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = []
    // 大于0 代表该月不是从周日开始 即该月前有空格显示
    if (firstDayOfWeek > 0) {
      // 比如说周六  我周五之前就需要显示空 遍历往数组中添加五项 代表空
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i)
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },

  // 创建该月日期
  calculateDays(year, month, list) {
    let days = []
    const thisMonthDays = this.getThisMonthDays(year, month)
    for (let i = 1; i <= thisMonthDays; i++) {
      // 获取签到数组 判断 签到的日期中添加is标识
      if (list.indexOf(i) > -1) {
        days.push({
          time: i,
          isSign: 'is'
        })
      } else {
        days.push({
          time: i,
          isSign: 'no'
        })
      }
    }
    this.setData({
      days
    })
  }
})